<template>
  <div class="card-items">
    <div class="head-part">
      <div class="title">
        <img src="@/assets/van-img.png" alt="" />
        <span>产品满意度排名</span>
      </div>
      <div class="value">{{ cityName + "   " + proValue }}</div>
    </div>

    <div class="order-item" v-for="(item, index) in listData" :key="index">
      <img v-if="index == 0" src="@/assets/pan-view/top1.png" />
      <img v-else-if="index == 1" src="@/assets/pan-view/top2.png" />
      <img v-else-if="index == 2" src="@/assets/pan-view/top3.png" />

      <div v-else class="order">{{ index + 1 }}</div>
      <div class="city">{{ item.city }}</div>
      <div class="bar" :style="'width:' + widths[index] + 'px'"></div>
      <div class="value">{{ item.value }}</div>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import { reactive, toRefs, onMounted } from "vue";
export default {
  props: ["itemObj"],
  setup(props) {
    let item = props.itemObj;
    const state = reactive({
      cityName: item.city,
      proValue: item.value,
      listData: item.listData,
      widths: [],
    });

    /**
     * item:需要计算的item
     * index：需要计算的index
     * firstItem：数组第一个元素
     * lastItem：数组最后一个元素
     * length：数组长度
     */
    function getWidth(item, index, firstItem, lastItem, length) {
      let width = window.innerWidth;
      let marginX =
        length > 17 ? 0 : firstItem.value - lastItem.value > 30 ? 0 : 10;
      let viewW =
        (item.value / firstItem.value) * (width - 130 - index * marginX); // 防止数值差距较小，柱状图差距不明显
      return viewW;
    }
    onMounted(() => {
      let listData = state.listData;
      if (listData.length <= 0) {
        Toast("无地市排名数据");
        return;
      }
      let firstItem = listData[0];
      let lastItem = listData[listData.length - 1];
      let widths = [];
      for (let index = 0; index < listData.length; index++) {
        const element = listData[index];
        let width = getWidth(
          element,
          index,
          firstItem,
          lastItem,
          listData.length
        );
        widths.push(width);
      }
      state.widths = widths;
    });
    return {
      ...toRefs(state),
    };
  },
};
</script>
<style lang="scss" scoped>
.card-items {
  margin: 10px;
  width: calc(100% - 20px);
  //   height: 524px;
  background-color: #fff;
  border-radius: 5px;
  padding: 10px 0 2px 10px;
  margin-bottom: 10px;
  .head-part {
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    .qs-text {
      padding: 0 15px;
      line-height: 24px;
      color: #666666;
      margin-right: 10px;
      font-size: 12px;
      background: #ffffff;
      box-shadow: 0px 0px 2px 0px rgba(0, 0, 0, 0.2);
      border-radius: 4px;
    }
  }
  .title {
    width: 50%;
    display: flex;
    align-items: center;
    span {
      font-size: 16px;
      font-weight: 400;
      color: #333333;
    }
  }

  .value {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 96px;
    height: 24px;
    background: #fffaf4;
    border-radius: 4px;
    font-size: 14px;
    font-family: Helvetica-Bold, Helvetica;
    font-weight: bold;
    color: #fe6a00;
    line-height: 17px;
  }

  .order-item {
    display: flex;
    height: 40px;
    align-items: center;

    img {
      height: 24px;
      width: 24px;
    }

    .order {
      height: 24px;
      width: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      font-family: Helvetica-Bold, Helvetica;
      font-weight: bold;
      color: #666666;
      line-height: 14px;
      background: url("@/assets/pan-view/top其他.png") no-repeat 0 0;
      background-size: 100% 100%;
    }

    .city {
      width: 64px;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #333333;
      line-height: 20px;
      text-align: center;
    }

    .value {
      font-size: 14px;
      font-family: Helvetica-Bold, Helvetica;
      font-weight: bold;
      color: #333333;
      line-height: 16px;
    }

    .bar {
      height: 20px;
      border-radius: 15px;
      background: linear-gradient(270deg, #fe6800 0%, #ff9900 100%);
    }
  }
}
</style>
